#!/usr/bin/perl -w

use strict;
use Getopt::Long;

my ($prog,$progdir);
BEGIN {
    $0 =~ m{(.*)/(.*?)};
    ($progdir,$prog) = ($1,$2);
    $progdir = '.' unless $progdir;
    #print "$progdir\n";
    push @INC, "$progdir/lib", "$progdir";
    #print join(':',@INC),"\n";
}

$main::verbose = 1;

my $opts = {v=>1,q=>0};

if(! GetOptions($opts,'v+','q+')) { die usage(); }

$main::verbose = $opts->{v};
$main::verbose -= $opts->{q};

print "Setting verbosity (debugging) level to '$opts->{v}'.\n" if $opts->{v} > 1;

sub usage {
    ("Usage: $0 [-q] [-v [-v ...]]\n".
     " -v   increase verbosity.\n".
     " -q   decrease verbosity.\n"
    );
}

use XNAT::Connection;

print "$prog: Creating Connection:\n" if $main::verbose > 1;
my $xconn = XNAT::Connection->new(host=>'gspcentral');
print "$prog: OK\n" if $main::verbose > 1;

my $errors = 0;

my @sessions = $xconn->getAllSessionIDs();
if(!@sessions) {
    die "ERROR: No sessions found.\n";
}
my $count = 0;
foreach my $session_id (@sessions) {
    $count++;
    $xconn->getFCScanList({id=>$session_id}) || $errors ++;;
    #last if $count > 35;
}

if($errors) {
    print "ERROR: Something went wrong. Exiting non-zero.\n";
    exit 1;
}

exit;
__END__
